<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品货号" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入商品货号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入商品标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品副标题" prop="subTitle">
        <el-input
          v-model="queryParams.subTitle"
          placeholder="请输入商品副标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类主键" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入分类主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架状态" prop="publishStatus">
        <el-select v-model="queryParams.publishStatus" placeholder="请选择上架状态" clearable>
          <el-option
            v-for="dict in dict.type.publish_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上架时间" prop="publishTime">
        <el-date-picker clearable
          v-model="queryParams.publishTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择上架时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supermarket:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supermarket:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template #default="props">

          <el-row>
            <el-col :span="24" class="card-box">
              <el-card>
                <div slot="header">
                  <span><i class="el-icon-goods"></i> 商品信息</span>
                </div>
                <div class="el-table el-table--enable-row-hover el-table--medium">
                  <table cellspacing="0" style="width: 100%;">
                    <tbody>
                      <tr>
                        <td class="el-table__cell"><div class="cell">商品标题</div></td>
                        <td class="el-table__cell"><div class="cell">{{ props.row.product.title }}</div></td>
                        <td class="el-table__cell"><div class="cell">商品副标题</div></td>
                        <td class="el-table__cell"><div class="cell">{{ props.row.product.subTitle }}</div></td>
                        <td class="el-table__cell"><div class="cell">关键字</div></td>
                        <td class="el-table__cell"><div class="cell">{{ props.row.product.keywords }}</div></td>
                      </tr>
                      <tr>
                        <td class="el-table__cell"><div class="cell">销量</div></td>
                        <td class="el-table__cell"><div class="cell">{{ props.row.product.sale }}</div></td>
                        <td class="el-table__cell"><div class="cell">审核状态</div></td>
                        <td class="el-table__cell"><div class="cell"><dict-tag :options="dict.type.audit_status" :value="props.row.product.auditStatus"/></div></td>
                        <td class="el-table__cell"><div class="cell">审核备注</div></td>
                        <td class="el-table__cell"><div class="cell">{{ props.row.product.auditRemark }}</div></td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </el-card>
            </el-col>
          
            <el-col :span="24" class="card-box">
              <el-card>
                <div slot="header">
                  <span><i class="el-icon-goods"></i> 商品属性值</span>
                </div>
                <div class="el-table el-table--enable-row-hover el-table--medium">
                  <el-table :data="props.row.productAttributeList" :border="true">
                    <el-table-column label="主键" align="center" prop="id" />
                    <el-table-column label="商品货号" align="center" prop="sku" />
                    <el-table-column label="商品标题" align="center" prop="title" />
                    <el-table-column label="可选值列表，以逗号隔开" align="center" prop="inputList" />
                  </el-table>
                </div>
              </el-card>
            </el-col>

            <el-col :span="24" class="card-box">
              <el-card>
                <div slot="header">
                  <span><i class="el-icon-goods"></i> 商品规格</span>
                </div>
                <div class="el-table el-table--enable-row-hover el-table--medium">
                  <el-table :data="props.row.productSkuList" :border="true">
                    <el-table-column label="主键" align="center" prop="id" />
                    <el-table-column label="商品货号" align="center" prop="sku" />
                    <el-table-column label="商品规格" align="center" prop="skuCode" />
                    <el-table-column label="图片" align="center" prop="pic" width="100">
                      <template slot-scope="scope">
                        <image-preview :src="scope.row.pic" :width="50" :height="50"/>
                      </template>
                    </el-table-column>
                    <el-table-column label="价格" align="center" prop="price" />
                    <el-table-column label="商品销售属性" align="center" prop="spData" />
                    <el-table-column label="销量" align="center" prop="sale" />
                  </el-table>
                </div>
              </el-card>
            </el-col>

          </el-row>
  
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="product.id" />
      <el-table-column label="商品货号" align="center" prop="product.sku" />
      <el-table-column label="商品标题" align="center" prop="product.title" />
      <el-table-column label="图片" align="center" prop="product.pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.product.pic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="product.price" />
      <el-table-column label="分类" align="center" prop="product.categoryName" />
      <el-table-column label="排序" align="center" prop="product.sort" />
      <el-table-column label="审核状态" align="center" prop="product.auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.product.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="product.publishStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.publish_status" :value="scope.row.product.publishStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="上架时间" align="center" prop="product.publishTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.product.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleCommit(scope.row)"
            v-show="scope.row.product.auditStatus == 0"
            v-hasPermi="['supermarket:product:commit']"
          >提交审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleAudit(scope.row)"
            v-show="scope.row.product.auditStatus == 1"
            v-hasPermi="['supermarket:product:audit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handlePublish(scope.row)"
            v-show="scope.row.product.auditStatus == 2"
            v-hasPermi="['supermarket:product:publish']"
          >上下架</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supermarket:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supermarket:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核商品对话框 -->
    <auditProductForm ref="auditProductForm"></auditProductForm>
    <!-- 上下架商品对话框 -->
    <publishProductForm ref="publishProductForm"></publishProductForm>
  </div>
</template>

<script>
import { listProduct, commitProduct, delProduct } from "@/api/supermarket/product";
import { listCategory} from "@/api/supermarket/category";
import auditProductForm from './auditProductForm.vue';
import publishProductForm from './publishProductForm.vue';
export default {
  name: "Product",
  dicts: ['delete_status', 'audit_status', 'publish_status'],
  components: {
    auditProductForm,
    publishProductForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sku: null,
        title: null,
        subTitle: null,
        keywords: null,
        categoryId: null,
        publishStatus: null,
        publishTime: null,
        auditStatus: null,
      },

      // 分类树选项
      categoryOptions: [],
 
    };
  },
  created() {

    listCategory().then(response => {
      let map  = new Map();
      response.data.forEach(element => {
        map.set(element.id,element);
      });
      this.categoryOptions = map;
    });

    this.getList();
  },
  watch: {
    /**
     * 观察路由变化，如果是添加或修改商品，则刷新数据
     * @param {*} to    到哪去
     * @param {*} from  从哪来
     */
    $route(to,from){
      if(from != null && from.name == "ProductEdit"){
        this.queryParams.pageNum = Number(from.query.pageNum);
        this.getList();
      }
    }
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.productList.forEach(element => {
          element.product.categoryName = this.categoryOptions.get(element.product.categoryId).name;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      const params = { pageNum: this.queryParams.pageNum };
      this.$tab.openPage("添加商品信息", '/supermarket/product-edit/index/0', params);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.product.id || this.ids;
      const params = { pageNum: this.queryParams.pageNum };
      this.$tab.openPage("修改商品信息", '/supermarket/product-edit/index/' + id, params);
    },
    /** 提交审核按钮操作 */
    handleCommit(row) {
      this.$modal.confirm('是否确认提交 '+row.product.sku+' 审核', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        let commitData = {
          sku : row.product.sku,
          auditStatus: 1   // 0 未提交 1待审核
        }
        commitProduct(commitData).then(response => {}); 
      }).then(response => {
          this.getList();
          this.$modal.msgSuccess(msg+"成功");
        }).catch(() => {});
    },
     /** 审核按钮操作 */
     handleAudit(row) {
      this.$refs.auditProductForm.handleAudit(row);
    }, 

    /** 上下架按钮操作 */
    handlePublish(row) {
      this.$refs.publishProductForm.handlePublish(row);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.product.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supermarket/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
